/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tssp_internal.h"
#include "minunit.h"
#include "tssp_dvb_sdt.h"

#define PREF                            "tssp_dvb_sdt"
#define TO_NETWORK_ID(data)             (((data)[0] << 8) + ((data)[1]))


static uint8_t data0[] = {
#if 0
    0x46, // table_id(8)
    0xF0, 0x00, // 1111XXXX XXXXXXXX
    0x00, 0x01, // transport_stream_id(16)
    0xC3, // 1100 0011 reserved(2), version_number(5), current_next_indicator(1)
    0x00, // section_number(8)
    0x00, // last_section_number(8)
#endif

    0x00, 0xAA, // original_network_id(16)
    0xFF, // reserved_future_use(8)

    0x12, 0x34, // service_id(16)
    0xFD, // reserved_future_use(6), EIT_schedule_flag(1), EIT_present_following_flag(1)
    0x90, 0x00,  // running_status(3), free_CA_mode(1), descriptors_loop_length(12)

    0x12, 0x34, // service_id(16)
    0xFD, // reserved_future_use(6), EIT_schedule_flag(1), EIT_present_following_flag(1)
    0x90, 0x00,  // running_status(3), free_CA_mode(1), descriptors_loop_length(12)
};

static
const char *test_tssp_dvb_sdt_parse()
{
    struct tssp_dvb_sdt_service *sdt_service;
    struct tssp_dvb_sdt *sdt;
    struct tssp_section src = {
        .table_id = 0x42,
        .syntax_indicator = 1,
        .private_indicator = 1,
        .length = sizeof(data0),

        .data = {
            .start = data0,
            .end = data0 + sizeof(data0),
        },

        .extension = 0x0001,
        .version = 1,
        .current_next = 1,
        .number = 0,
        .last_number = 0,

        .next_section = NULL,
    };

    sdt = tssp_dvb_sdt_parse(&src);

    mu_assert(PREF "parse", sdt != NULL);
    mu_assert(PREF "parse - ts_id", 0x0001 == sdt->ts_id);
    mu_assert(PREF "parse - version", 1 == sdt->version);

    mu_assert(PREF "parse - network_id", 0x00AA == sdt->network_id);

    STAILQ_FOREACH(sdt_service, &sdt->services, entry) {
        mu_assert(PREF "parse - service_id", 0x1234 == sdt_service->service_id);
        mu_assert(PREF "parse - eit_schedule", 0 == sdt_service->eit_schedule);
        mu_assert(PREF "parse - eit_present", sdt_service->eit_present);
        mu_assert(PREF "parse - running_status", 4 == sdt_service->running_status);
        mu_assert(PREF "parse - free_ca_mode", sdt_service->free_ca_mode);
    }

    tssp_dvb_sdt_destroy(sdt);

    return NULL;
}

const char *test_tssp_dvb_sdt()
{
    mu_run_test(test_tssp_dvb_sdt_parse);
    return NULL;
}

